package fr.umlv.escapeir.gesture;

import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;


public class Gesture {
	private final ArrayList<Integer> X;
	private final ArrayList<Integer> Y;
	private int nbElement;
	
	private Mask mask = null;
	private int value = -1;
	private Direction direction = Direction.UNDEFINED;
	private boolean isValidMask = false;
	
	private Point2D origin;
	private Point2D end;
	
	private boolean isFinished = false;
	private int print = 20;
	
	/**
	 * 
	 */
	public Gesture() {
		X = new ArrayList<Integer>();
		Y = new ArrayList<Integer>();
		nbElement = 0;
	}

	public Mask getMask() {
		return mask;
	}

	public int getValue() {
		return value;
	}

	public Direction getDirection() {
		return direction;
	}
	
	public boolean isValidMask() {
		return isValidMask;
	}
	
	public boolean isFinished() {
		return isFinished;
	}
	
	public Point2D getOrigin() {
		return this.origin;
	}
	
	public Point2D getEnd() {
		return this.end;
	}

	public void setGestureValid(Mask mask, Direction direction, int value) {
		int endP = nbElement - 1;
		origin = new Point2D.Double(X.get(0), Y.get(0));
		end = new Point2D.Double(X.get(endP), Y.get(endP));
		
		this.mask = mask;
		this.value = value;
		this.direction = direction;
		this.isValidMask = true;
	}
	
	public void setGestureValid(Mask mask, Direction direction) {
		int endP = nbElement - 1;
		
		origin = new Point2D.Double(X.get(0), Y.get(0));
		end = new Point2D.Double(X.get(endP), Y.get(endP));
		
		this.mask = mask;
		this.direction = direction;
		this.isValidMask = true;
	}
	
	public void setIsFinished(boolean bool) {
		this.isFinished = bool;
	}
	
	/**
	 * @param x
	 * @param y
	 */
	public void addPoint(int x, int y) {
		X.add(x);
		Y.add(y);
		nbElement++;
	}

	/**
	 * @param index
	 * @return
	 */
	public int getX(int index) {
		if(index > nbElement - 1)
			throw new IndexOutOfBoundsException("Polyline X");
		return X.get(index);
	}

	/**
	 * @param index
	 * @return
	 */
	public int getY(int index) {
		if(index > nbElement - 1)
			throw new IndexOutOfBoundsException("Polyline Y");
		return Y.get(index);
	}

	/**
	 * @return
	 */
	public int size() {
		return nbElement;
	}

	/**
	 * @return
	 *
	public int[] toArrayIntX() {
		int[] tab = new int[nbElement];
		for(int i = 0; i < nbElement; i++)
			tab[i] = X.get(i);
		return tab;
	}

	/**
	 * @return
	 *
	public int[] toArrayIntY() {
		int[] tab = new int[nbElement];
		for(int i = 0; i < nbElement; i++)
			tab[i] = Y.get(i);
		return tab;
	}*/

	/**
	 * @return
	 */
	public int indexMinX() {
		int min = X.get(0);
		int index = 0;

		for(int i = 1; i < nbElement; i++) {
			int tmp = X.get(i);
			if(min > tmp) { 
				index = i;
				min = tmp;
			}
		}
		return index;
	}

	/**
	 * @return
	 */
	public int indexMaxX() {
		int max = X.get(0);
		int index = 0;

		for(int i = 1; i < nbElement; i++) {
			int tmp = X.get(i);
			if(max < tmp) {
				index = i;
				max = tmp;
			}
		}
		return index;
	}

	/**
	 * @return
	 */
	public int indexMinY() {
		int min = Y.get(0);
		int index = 0;

		for(int i = 1; i < nbElement; i++) {
			int tmp = Y.get(i);
			if(min > tmp) { 
				index = i;
				min = tmp;
			}
		}
		return index;
	}

	/**
	 * @return
	 */
	public int indexMaxY() {
		int max = Y.get(0);
		int index = 0;

		for(int i = 1; i < nbElement; i++) {
			int tmp = Y.get(i);
			if(max < tmp) {
				index = i;
				max = tmp;
			}
		}
		return index;
	}

	/**
	 * 
	 */
	public void clear() {
		if(print > 0) {
			print --;
			return;
		}
		for(int i = nbElement - 1; i >= 0; i--) {
			X.remove(i);
			Y.remove(i);
		}
		this.nbElement = 0;
	}
	
	public void drawGesture(Graphics2D g) {
		if(this.nbElement < 2) return;
		for(int i = 1; i < this.nbElement; i++) {
			g.drawLine(X.get(i - 1), Y.get(i - 1), X.get(i), Y.get(i));
		}
	}
	
	/*
	 * A SUPPRIMER ??
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 *
	@Override
	public String toString() {
		return "Polyline \n" +
				"X=" + X + " \n" +
						"Y=" + Y + "";
	}*/
}